package cn.edu.cdu.taobao.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class HttpBrowser {
	/**
	 * 
	 * @param url
	 * @return
	 */
	public static String getUrl(String url,String referer) {
		StringBuilder html=new StringBuilder();
		
		HttpClient httpClient=new DefaultHttpClient();
		
		
		try {
			HttpGet httpGet=new HttpGet(url);
			
			httpGet.setHeader("Referer", referer);
			HttpResponse response=httpClient.execute(httpGet);
			HttpEntity entity=response.getEntity();
			InputStream inputStream=entity.getContent();
			BufferedReader bReader=new BufferedReader(new InputStreamReader(inputStream));
			String len="";
			while ((len=bReader.readLine())!=null) {
				html.append(len);
				html.append("\n");
			}
			if (inputStream!=null) {
				inputStream.close();
			}
			if (bReader!=null) {
				bReader.close();
			}
			
			
		}catch (Exception e) {
			System.out.println(e.toString());
		}
		return html.toString();
		
	}
	
	public static String getUrl(String url) {
		StringBuilder html=new StringBuilder();
		
		HttpClient httpClient=new DefaultHttpClient();
		try {
			HttpGet httpGet=new HttpGet(url);
			HttpResponse response=httpClient.execute(httpGet);
			HttpEntity entity=response.getEntity();
			InputStream inputStream=entity.getContent();
			BufferedReader bReader=new BufferedReader(new InputStreamReader(inputStream));
			String len="";
			//String u8 ;
			while ((len=bReader.readLine())!=null) {
				//System.out.println(len);
				//u8= new String(len.getBytes("gbk"),"utf-8");
				html.append(len);
				html.append("\n");
			}
			if (inputStream!=null) {
				inputStream.close();
			}
			if (bReader!=null) {
				bReader.close();
			}
			
			
		}catch (Exception e) {
			System.out.println(e.toString());
		}
		return html.toString();
		
	}
	public static Document getByJsoup(String url){
		Document document = null;
		try {
			document = Jsoup
					.connect(url)
					.header("User-Agent",
							"Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0")
					.header("Accept-Language",
							"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3")
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")		
					.get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return document;
		
	}
}
